package com.xly.webclick.vm;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.animation.LinearInterpolator;

import com.xly.webclick.R;
import com.xly.webclick.base.BaseViewModel;
import com.xly.webclick.vi.ToolbarFeature;
import com.xly.webclick.view.RegisterActivity;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by xuleyuan on 2018/8/18
 */

public class RegisterViewModel extends BaseViewModel implements ToolbarFeature {

    public final ObservableField<String> phoneCode = new ObservableField<>("62");//手机归属前缀

    public final ObservableField<String> phone = new ObservableField<>("");//手机号

    public final ObservableBoolean btnClickAble = new ObservableBoolean(true);//按钮是否可以点击

    public final ObservableBoolean sent = new ObservableBoolean(false);//验证码发送成功

    public final ObservableBoolean verificationVisible = new ObservableBoolean(false);//验证码输入框是否显示

    public final ObservableInt textStatus = new ObservableInt(0);//按钮文字的显示控制

    public final ObservableInt sendStatusText = new ObservableInt(0);//按钮下方发送状态的显示控制

    public final ObservableBoolean loading = new ObservableBoolean(false);//加载旋转的显示控制

    public final ObservableField<String> countDown = new ObservableField<>("");//倒计时的控制

    public RegisterViewModel(RegisterActivity view) {
        super(view);
    }

    EventHandler eventHandler = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                ((Throwable) data).printStackTrace();
            }
        }
    };


    @Override
    protected void initData(Bundle savedInstanceState) {
        SMSSDK.registerEventHandler(eventHandler);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }

    @Override
    public void onResume() {

    }
    public void inputFinish(String s) {
//        if (checkSMSCodePresenter == null || phoneCode == null || phone == null) {
//            return;
//        }
//        checkSMSCodePresenter
//                .putParams("country_code", phoneCode.get())
//                .putParams("phone", phone.get())
//                .putParams("code", s)
//                .putParams("solecode", UUIDs.getUUID());
//        checkSMSCodePresenter.initResFromServer();
    }

    public void sendSMS() {
        SMSSDK.getVerificationCode(phoneCode.get(), phone.get(), null, (s, s1) ->
                TextUtils.isEmpty(s) || TextUtils.isEmpty(s1));
        textStatus.set(1);//发送中
        btnClickAble.set(false);
    }

    public void onPhoneCodeClick() {

    }

    @Override
    public void back() {

    }

    @Override
    public int icon() {
        return 0;
    }

    @Override
    public String rightTitle() {
        return null;
    }

    @Override
    public String setTitle() {
        return null;
    }

    @Override
    public void rightClick() {

    }

    @Override
    public boolean isRightTitleInVisible() {
        return false;
    }

    public String getBtnText(int state) {
        switch (state) {
            case 0:
                loading.set(false);
                if (getView() == null) {
                    return "";
                }
                return getActivity().getResources().getString(R.string.get_verification_code);
            case 1:
                loading.set(true);
                return "";
            case 2:
                loading.set(false);
                ValueAnimator valueAnimator = ValueAnimator.ofInt(60000, 0);
                valueAnimator.setDuration(60000);
                valueAnimator.setInterpolator(new LinearInterpolator());
                btnClickAble.set(false);
                valueAnimator.addUpdateListener(animation -> {
                    int a = (int) animation.getAnimatedValue() / 1000;
                    countDown.set(String.valueOf(a));
                    if (a == 0) {
                        valueAnimator.cancel();
                    }
                });
                valueAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        initText();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        initText();
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                valueAnimator.start();
                if (getView() == null) {
                    return "";
                }
                return getActivity().getResources().getString(R.string.resend_verfication_code);
            default:
                if (getView() == null) {
                    return "";
                }
                return getActivity().getResources().getString(R.string.get_verification_code);
        }
    }

    public String getStatusText(int status) {
        switch (status) {
            case 0:
                return "";
            case 1:
                return getActivity().getResources().getString(R.string.phone_number_format_error);
            case 2:
                return getActivity().getResources().getString(R.string.code_error);
            case 3:
                return getActivity().getResources().getString(R.string.request_too_many_times);
        }
        return "";
    }
    private void initText() {
        btnClickAble.set(true);
        sent.set(false);
        countDown.set("");
        textStatus.set(0);//发送失败回到初始
    }
}
